package com.muchfish.webflux.api;

import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

@Component
public class MyHandler {

    public Mono<ServerResponse> handleResource(ServerRequest request) {
        // 处理 GET 请求逻辑
        // 返回 Mono<ServerResponse>
        String resourceId = request.pathVariable("id");
        WebClient webClient = WebClient.create("http://localhost:8080");

        webClient.get()
                .uri("/api/resource/{id}", 123)
                .retrieve()
                .bodyToMono(String.class)
                .subscribe(response -> System.out.println("Response: " + response));
        // 处理逻辑
        return ServerResponse.ok().bodyValue("Resource ID: " + resourceId);
    }

    public Mono<ServerResponse> createResource(ServerRequest request) {
        // 处理 POST 请求逻辑
        // 返回 Mono<ServerResponse>
        return Mono.just("Hello, WebFlux!")
                .flatMap(response -> ServerResponse.ok().bodyValue(response));
    }
}
